package com.jmx.mstcodesandbox.controller;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/docker")
public class TestDockerController {
    @Resource
    private DockerClient dockerClient;

    /**
     * 获取docker镜像
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/pull")
    public String dockerPull() throws InterruptedException {
        // 测试获取docker镜像
        String image = "nginx:latest";
        PullImageCmd pullImageCmd = dockerClient.pullImageCmd(image);
        PullImageResultCallback pullImageResultCallback = new PullImageResultCallback(){
            @Override
            public void onNext(PullResponseItem item) {
                System.out.println("下载镜像："+item.getStatus());
                super.onNext(item);
            }
        };
        pullImageCmd.exec(pullImageResultCallback).awaitCompletion();
        System.out.println("下载完成");
        return "success dockerPull";
    }

    /**
     * 通过镜像创建docker容器
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/createContainer")
    public String dockerCreateContain() throws InterruptedException {
       String image = "nginx:latest";
        CreateContainerCmd containerCmd = dockerClient.createContainerCmd(image);
        CreateContainerResponse createContainerResponse = containerCmd.exec();
        System.out.println("createContainerResponse = " + createContainerResponse);
        System.out.println("createContainerResponse的ID = " + createContainerResponse.getId());
        return "success dockerCreateContain";
    }

    /**
     * 查看所有的docker容器信息
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/listAllContainer")
    public String dockerListContain() throws InterruptedException {
        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
        List<Container> containerList = listContainersCmd.withShowAll(true).exec();
        for (Container container : containerList) {
            System.out.println(container);
        }
        return "success dockerListContain";
    }

    /**
     * 启动容器并且查看日志
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/runAndLog")
    public String runAndLog(String id) throws InterruptedException {
        dockerClient.startContainerCmd(id).exec();
        // 查看日志
        LogContainerResultCallback logContainerResultCallback = new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                System.out.println(item.getStreamType());
                System.out.println("日志：" + new String(item.getPayload()));
                super.onNext(item);
            }
        };

        // 阻塞等待日志输出
        dockerClient.logContainerCmd(id)
                .withStdErr(true)
                .withStdOut(true)
                .exec(logContainerResultCallback)
                .awaitCompletion();
        return "success runAndLog";
    }

    /**
     * 删除容器
     * @param id 容器id
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/removeContainer")
    public String removeContainer(String id) throws InterruptedException {
        dockerClient.removeContainerCmd(id).withForce(true).exec();
        return "success removeContainer";
    }

    @RequestMapping("/removeImage")
    public String removeImage(String id) throws InterruptedException {
        dockerClient.removeImageCmd(id).withForce(true).exec();
        return "success removeImage";
    }

}
